
exports.controllers={
	/*************************************************************************/
	market_vendors_list: function(space,options) {
		if (!options) options={};
		space.options=options;
		if (space.fields.vendor_id) {
			space.market_vendor=space.mfact.get("market_vendor",space.fields.vendor_id);
			return space.views.process("widgets/"+(options.template_view||"market_vendors/view"),space);
		}
		space.market_vendors=space.mfact.list("market_vendor",{enabled:1,order_by:"main.ordering"});
		return space.views.process("widgets/"+(options.template_list||"market_vendors/list"),space);
	},

	/*************************************************************************/
	market_categories_list: function(space,options) {
		if (!options) options={};
		space.options=options;
		var pid;
		if (options.parent_id) pid=options.parent_id; else pid={type:"null"};
		space.market_categories=space.mfact.list("market_category",{enabled:1,parent_id:pid,order_by:"main.ordering"});
		return space.views.process("widgets/"+(options.template_list||"market_categoriess/list"),space);
	},

	/*************************************************************************/
	market_catalog1: function(space) {
		if (space.fields.mcatcode) {
			var h={calc_code:space.fields.mcatcode};
			space.fields.mcatid=space.site.sql.execute_and_fetch_one("market_categories/get_by_calc_code",h).id;
		}
		if (space.fields.mitemcode) {
			var h={calc_code:space.fields.mitemcode};
			space.fields.mitemid=space.site.sql.execute_and_fetch_one("market_items/get_by_calc_code",h).id;
		}
		var mode=space.fields.mode||"list";
		if (mode=="list" && space.fields.mitemid) mode="view";

		return space.controllers['market_catalog1__'+mode](space);
	},

	/*************************************************************************/
	market_catalog1__list: function(space) {
		space.market_categories=space.mfact.list_hier("market_category",{});
		space.market_categories.forEach(function(x) {
			if (x.id==space.fields.mcatid) x.active=1;
		});
		
		var order_by="main.name";
		var filters={order_by: order_by,enabled: 1};
		
		/* categories */
		var cats="";
		var selcats=space.selcats=space.mfact.list_hier("market_category",{},space.fields.mcatid);
		selcats.forEach(function(x) {
			if (cats.length) cats+=",";
			cats+=x.id;
		});
		filters.category_id={type: "in",value: cats};

		/* vendors */
//		space.market_vendors=space.mfact.list("market_vendor",{});
		space.search_market_vendors=space.site.sql.execute_and_fetch_single("select v.id,v.name from Tmarket_vendors v where exists (select 1 from Tmarket_items i where category_id in ("+cats+") and i.vendor_id=v.id and i.enabled=1) and v.enabled=1 order by v.name");
		if (space.fields.vnd) {
			var vnds={};
			if (typeof space.fields.vnd=="object") {
				filters.vendor_id={type: "in",value: space.fields.vnd};
				space.fields.vnd.forEach(function(v) {
					vnds[v]=1;
				});
			} else {
				filters.vendor_id=space.fields.vnd;
				vnds[space.fields.vnd]=1;
			}
			space.search_market_vendors.forEach(function(v) {
				if (vnds[v.id]) v.active=1;
			});
		}
	
		/* prices */
		space.search_market_price=space.site.sql.execute_and_fetch_single("select min(price) as minval,max(price) as maxval from Tmarket_items i where category_id in ("+cats+") and i.enabled=1")[0];
		space.search_market_price.cmin=space.search_market_price.min=Math.floor(space.search_market_price.minval||0);
		space.search_market_price.cmax=space.search_market_price.max=Math.ceil(space.search_market_price.maxval||0);
		if (space.fields.price_min) {
			space.search_market_price.cmin=space.fields.price_min;
			space.search_market_price.cmax=space.fields.price_max;
			filters.price={type:"between",value1:space.fields.price_min,value2:space.fields.price_max};
		}

		/* params */
		space.search_market_params=space.site.sql.execute_and_fetch("market_category_params/list_for_search",{category_id:space.fields.mcatid});
		space.search_market_params.forEach(function(p) {
			switch (p.datatype_code) {
				case "number":
				case "integer":
					var minmax=space.site.sql.execute_and_fetch_one("market_item_param_values/get_min_max_number",{param_id:p.id,category_id:space.fields.mcatid});
					p.code="range";
					p.cmin=p.min=Math.floor(minmax.minval||0);
					p.cmax=p.max=Math.ceil(minmax.maxval||0);
					if (space.fields["par_"+p.id+"_min"]) {
						p.cmin=space.fields["par_"+p.id+"_min"];
						p.cmax=space.fields["par_"+p.id+"_max"];
						filters["par_"+p.id]={type: "exists",value:"select 1 from Tmarket_item_param_values where item_id=main.id and param_id="+p.id+" and value_number>="+parseFloat(space.fields["par_"+p.id+"_min"])+" and value_number<="+parseFloat(space.fields["par_"+p.id+"_max"])};
					}
					break;
				case "varchar":
				case "select":
					p.code="cb";
					p.values=space.site.sql.execute_and_fetch("market_item_param_values/list_available_text",{param_id:p.id});
					if (space.fields["par_"+p.id]) {
						var vals={};
						if (typeof space.fields["par_"+p.id]=="object") {
							space.fields["par_"+p.id].forEach(function(v) {
								vals[v]=1;
							});
						} else {
							vals[space.fields["par_"+p.id]]=1;
						}
						var valids="";
						p.values.forEach(function(v) {
							if (vals[v.id]) {
								v.active=1;
								if (valids) valids+=",";
								valids+=v.id;
							}
						});
						filters["par_"+p.id]={type: "exists",value: "select 1 from Tmarket_item_param_values pv1,Tmarket_item_param_values pv2 where pv2.id in ("+valids+") and pv1.item_id=main.id and pv1.param_id="+p.id+" and pv1.value_text=pv2.value_text"};
					}
					break;
			}
		});
//		throw space.Dumper(filters);
		space.market_items=space.mfact.list("market_item",filters);
		space.output=space.views.process("widgets/market_catalog1/list",space);
	},

	/*************************************************************************/
	market_catalog1__view: function(space) {
		space.market_item_images=[];
		space.market_item=space.mfact.get("market_item",space.fields.mitemid);
		var variants=space.mfact.list("market_item",{parent_id:space.fields.mitemid,in_stock:1});
		if (variants.length) space.market_item.variants=variants;
		var p=space.market_item.category_FK();
		space.categories_path=[];
		while (p) {
			space.categories_path.unshift(p);
			p=p.parent_FK();
		}
		space.functions.market_catalog1__view__load_data(space,space.market_item);
		variants.forEach(function(item) {
			space.functions.market_catalog1__view__load_data(space,item,space.market_item);
		});
		if (space.market_item_images.length) {
			space.market_item_image=space.market_item_images[0];
		}
		space.output=space.views.process("widgets/market_catalog1/view",space);
	},
	market_catalog1__add_to_cart: function(space) {
		space.functions.market_catalog_cart_set(space,space.fields.item_id,space.fields.amount);
		return space.output=JSON.stringify({refresh:1});
	}
};

exports.functions={
	/*************************************************************************/
	market_catalog1__view__load_data: function(space,item,parent) {
		item.param_groups=space.mfact.list("market_category_param_group",{order_by: "main.ordering",category_id:item.category_id});
		item.param_groups.forEach(function(grp) {
			grp.params=space.site.sql.execute_and_fetch("market_item_param_values/list",{param_group_id:grp.id,item_id:space.fields.mitemid});
			grp.params.forEach(function(p) {
				p.show_short=1*p.show_short;
				if (p.show_short) grp.has_short=1;
				if (p.datatype_id==1) {
					if (p.value_number!=null && p.value_number!=undefined) p.value=p.value_number.replace(/0+$/,'').replace(/\.$/,''); else p.value="";
				} else {
					p.value=p.value_text;
				}
			});
		});
		item.image_group().forEach(function(x) {
			space.market_item_images.push(x);
		});
	},

	/*************************************************************************/
	market_catalog_cart_auth: function(space) {
	},

	/*************************************************************************/
	market_catalog_cart_set: function(space,item_id,amount) {
		var cart;
		if (!space.uid) {
			var sid=space.req.cookie.marketsid;
			if (!sid) {
				sid=space.functions.mksid();
				var exp=undefined;
				space.res.cookie("marketsid",sid,exp,'/',space.site.cookie_domain);
			}
			cart=space.site.sql.execute_and_fetch_one("market_carts/get_fill_cart_by_sid",{sid:sid});
			if (!cart) {
				space.site.sql.execute("market_carts/insert_by_sid",{sid:sid});
				cart=space.site.sql.execute_and_fetch_one("market_carts/get_fill_cart_by_sid",{sid:sid});
			}
		} else {
			cart=space.site.sql.execute_and_fetch_one("market_carts/get_fill_cart_by_user",{user_id:space.uid});
			if (!cart) {
				space.site.sql.execute("market_carts/insert_by_user",{user_id:space.uid});
				cart=space.site.sql.execute_and_fetch_one("market_carts/get_fill_cart_by_user",{user_id:space.uid});
			}
		}
		var cart_item=space.site.sql.execute_and_fetch_one("market_cart_items/get_by_item",{cart_id:cart.id,item_id:item_id});
		var item=space.mfact.get("market_item",item_id);
		if (cart_item) {
			if (amount) {
				space.site.sql.execute("market_cart_items/update",{amount:amount,cart_item_id:cart_item.id});
			} else {
				space.site.sql.execute("market_cart_items/delete",{cart_item_id:cart_item.id});
			}
		} else if (amount!=0) {
			space.site.sql.execute("market_cart_items/insert",{amount:amount,cart_id:cart.id,price:item.price,currency_id:item.currency_id});
		}
	}
};

